<template>
    <el-dialog
            v-dialogDrag
            :title="type=='add'?'新增组织':'编辑组织'"
            :visible="show"
            @close="close"
            top="50px"
            width="500px">
        <div>
            <el-form :model="form" ref="addForm" :rules="rules" label-width="80px">
                <el-form-item label="上级组织">
                    <el-cascader
                            ref="tree"
                            clearable
                            style="width:400px;"
                            v-model="form.upperId"
                            :options="options"
                            :props="{ expandTrigger: 'hover',emitPath:false,checkStrictly: true,label:'name',value:'id',children:'childs' }"
                            ></el-cascader>
                </el-form-item>
                <el-form-item label="组织名称" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model.number="form.sort"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio v-model="form.status" label="正常">正常</el-radio>
                    <el-radio v-model="form.status" label="停用">停用</el-radio>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="close">取 消</el-button>
            <el-button size="small" type="primary" @click="update">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
    export default {
        props:{
            show:{
                type:Boolean,
                default:false
            },
            type:{
                type:String,
                default:'add'
            },
            id:{
                type:String,
                default:''
            }
        },
        data(){
            return {
                form:{
                    name:'',
                    status:'正常',
                    sort:0,
                    upperId:''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '请选择状态', trigger: 'change'}
                    ],
                    sort:[
                        {type:'number',message:'请输入数字类型', trigger:'blur'}
                    ]
                },
                options: []
            }
        },
        mounted() {
          this.queryData()
          if(this.type == 'add') {
              if(this.id) {
                  this.form.upperId = this.id
              }
          }else{
              if(this.id) {
                  this.queryForm()
              }
          }
        },
        methods:{
            close(){
                this.$emit('update:show', false)
            },
            update() {
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                        if(this.type == 'add') {
                            if (this.form.upperId) {
                                let checked = this.$refs.tree.getCheckedNodes()
                                let root = checked.length > 0? checked[0] : null
                                let rootId = null
                                if(root) {
                                    while (root.parent) {
                                        root = root.parent
                                    }
                                    rootId = root.value
                                }
                                let sort = this.form.sort || 0
                                this.$httpProj.post('/proj/Organization/addChild?name=' + this.form.name + '&status=' + this.form.status + '&sort=' + sort + "&upperId=" + this.form.upperId + '&rootId='+ rootId + "&noProjectId=true").then(res => {
                                    this.$message({
                                        type: 'success',
                                        message: '添加成功'
                                    })
                                    this.$emit('success')
                                    this.close()
                                })
                            } else {
                                let sort = this.form.sort || 0
                                this.$httpProj.post('/proj/Organization/addParent?name=' + this.form.name + '&status=' + this.form.status + '&sort=' + sort+ "&noProjectId=true").then(res => {
                                    this.$message({
                                        type: 'success',
                                        message: '添加成功'
                                    })
                                    this.$emit('success')
                                    this.close()
                                })
                            }
                        }else{
                            let checked = this.$refs.tree.getCheckedNodes()
                            let root = checked.length > 0? checked[0] : null
                            let rootId = null
                            if(root) {
                                while (root.parent) {
                                    root = root.parent
                                }
                                rootId = root.value
                            }
                            let sort = this.form.sort || 0
                            this.$httpProj.put('/proj/Organization/update?name=' + this.form.name + '&status=' + this.form.status + '&sort=' + sort + "&upperId=" + this.form.upperId + "&id="+this.id + '&rootId='+ rootId+ "&noProjectId=true").then(res => {
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功'
                                })
                                this.$emit('success')
                                this.close()
                            })
                        }
                    }
                });
            },
            queryData() {
                this.$httpProj.get('/proj/Organization/getTree?noProjectId=true').then(res => {
                    let list = res.data.list
                    deleteExtraChild(list)
                    function deleteExtraChild(list) {
                        list.forEach(item => {
                            if(item.childs.length == 0) {
                                delete item.childs
                            }else{
                                deleteExtraChild(item.childs)
                            }
                        })
                    }
                    this.options = list
                })
            },
            queryForm() {
                this.$httpProj('/proj/Organization/getById?id='+this.id+"&noProjectId=true").then(res => {
                    this.form.name = res.data.name
                    this.form.status = res.data.status
                    this.form.sort = parseInt(res.data.sort)
                    this.form.upperId = res.data.parentId
                })
            }
        }
    }
</script>

<style lang="scss" scoped>

</style>
